﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.IO;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.Utility;

public partial class admin_vico_reginfo_list : AdminBasePage
{
    protected string dealerpstr = "";
    protected string dealercstr = "var dcitys = Array();\n";
    protected string dealerstr = "var dealers = Array();\n";
    protected string Dealer_provice = "";
    protected string Dealer_city = "";
    protected string Dealer = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Dealer_provice = Request.Params["dealerprovince"];
        Dealer_city = Request.Params["dealercity"];
        Dealer = Request.Params["dealer"];
        DataSet dealerds = new VicoDealerBll().GetAll();
        Hashtable dpt = new Hashtable();//省
        Hashtable dpc = new Hashtable();//市
        Hashtable dpd = new Hashtable();//经销商
        for (int i = 0; i < dealerds.Tables[0].Rows.Count; i++)
        {
            if (!dpt.ContainsKey(dealerds.Tables[0].Rows[i]["PROVINCE"]))
            {
                dpt[dealerds.Tables[0].Rows[i]["PROVINCE"]] = dealerds.Tables[0].Rows[i]["pcname"];
            }
            if (dpc.ContainsKey(dealerds.Tables[0].Rows[i]["PROVINCE"]))
            {
                Hashtable tc = ((Hashtable)dpc[dealerds.Tables[0].Rows[i]["PROVINCE"]]);
                tc[dealerds.Tables[0].Rows[i]["CITY"]] = dealerds.Tables[0].Rows[i]["ccname"];
            }
            else
            {
                Hashtable tt = new Hashtable();
                tt[dealerds.Tables[0].Rows[i]["CITY"]] = dealerds.Tables[0].Rows[i]["ccname"];
                dpc[dealerds.Tables[0].Rows[i]["PROVINCE"]] = tt;
            }

            if (dpd.ContainsKey(dealerds.Tables[0].Rows[i]["CITY"]))
            {
                Hashtable tc = ((Hashtable)dpd[dealerds.Tables[0].Rows[i]["CITY"]]);
                tc[dealerds.Tables[0].Rows[i]["ID"]] = dealerds.Tables[0].Rows[i]["DEALER_NAME"];
            }
            else
            {
                Hashtable tt = new Hashtable();
                tt[dealerds.Tables[0].Rows[i]["ID"]] = dealerds.Tables[0].Rows[i]["DEALER_NAME"];
                dpd[dealerds.Tables[0].Rows[i]["CITY"]] = tt;
            }

        }

        foreach (object obj in dpt.Keys)
        {
            string key = obj.ToString();
            dealerpstr += "<option value=\"" + key + "\">" + dpt[obj] + "</option>\n";
        }

        foreach (object obj in dpc.Keys)
        {
            string key = obj.ToString();
            Hashtable ti = (Hashtable)dpc[obj];
            dealercstr += "dcitys['" + key + "'] = [";
            int i = 0;
            foreach (object obj2 in ti.Keys)
            {
                dealercstr += "[" + obj2 + ",'" + ti[obj2] + "'" + "]";
                if (i < ti.Count - 2)
                {
                    dealercstr += ",";
                }
            }
            dealercstr += "]\n";
            //csi++;
        }

        foreach (object obj in dpd.Keys)
        {
            string key = obj.ToString();
            Hashtable ti = (Hashtable)dpd[obj];
            dealerstr += "dealers['" + key + "'] = [";
            int i = 0;
            foreach (object obj2 in ti.Keys)
            {
                dealerstr += "[" + obj2 + ",'" + ti[obj2] + "'" + "]";
                if (i < ti.Count - 2)
                {
                    dealerstr += ",";
                }
            }
            dealerstr += "]\n";
            //csi++;
        }
        if (!IsPostBack)
        {
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);

            
        }

        GridView1DataBind();
       
    }

    private VicoRegInfoModel GetQ()
    {
        VicoRegInfoModel q = new VicoRegInfoModel();
        q.User_name = username.Text;
        q.Mobile = moblie.Text;
        q.Camname = camname2.Value;
        q.Isincept = !string.IsNullOrEmpty(incept.Text) ? int.Parse(incept.Text) : 0;

        q.Dealer_provice = !string.IsNullOrEmpty(Dealer_provice) ? int.Parse(Dealer_provice) : 0;
        q.Dealer_city = !string.IsNullOrEmpty(Dealer_city) ? int.Parse(Dealer_city) : 0;
        q.Dealer = !string.IsNullOrEmpty(Dealer) ? int.Parse(Dealer) : 0;

        q.QStartDate = startdate.Text;
        q.QEndDate = enddate.Text;
        q.Sourec = Request.Params["source"];
        q.Channel = Request.Params["channel"];

        //arvato返回状态查询
        q.vico_send_ccc_resultCode = tbxStatus.Text.Trim();
        
        //按车型查询
        q.StrVicoModel = Request.Params["vicomodel"];
      
        return q;
    }

    private void GridView1DataBind()
    {
        
        int pagesize = 10;
        if (Request.Params["pageNum"] != null && Request.Params["pageNum"] != "")
        {
            try
            {
                GridView1.PageIndex = int.Parse(Request.Params["pageNum"]) - 1;
            }
            catch { }
        }
        if (Request.Params["rows"] != null && Request.Params["rows"] != "")
        {
            pagesize = int.Parse(Request.Params["rows"]);
            this.rows.Text = pagesize.ToString();
        }
        if (Request.Params["camname"] != null && Request.Params["camname"] != "")
        {
            camname2.Value = Request.Params["camname"];
        }
               

        PageTools pagetools = new VicoRegInfoBll().Query(GetQ(), GridView1.PageIndex+1, pagesize);

        GridView1.DataSource = pagetools.DataSet;
        GridView1.PageSize = pagesize;
        GridView1.DataBind();

        this.pagehtml.Text = getPageHtml(pagetools.PageCount(), pagetools.PageNo, pagetools.Counts);
    }

    
    protected void Mod_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], MOD);
        Response.Redirect("reginfo_detail.aspx?id=" + ((LinkButton)sender).CommandArgument + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Del_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], DEL);
        string ids = Request.Params["ids"];
        bool r = new VicoRegInfoBll().Del(ids);
        if (r)
        {
            this.insertLog("批量删除预约信息:" + ids);
            GridView1DataBind();
        }
    }

    protected void Imp_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], MOD);

        DataSet ds = new VicoRegInfoBll().QueryImp(GetQ());
        if (ds.Tables[0].Rows.Count != 0)
        {
            DataTable dt = ds.Tables[0];
            StringWriter sw = new StringWriter();
            //sw.WriteLine(Session["msg"].ToString() + "\t" );
            sw.Write("ID\t车型\t姓名\t称谓\t手机号码\t电子邮箱\t是否接收活动信息\t预计购买时间\t预计购买预算\t经销商省\t经销商市\t经销商\t活动\t来源\t渠道\t时间\tIP\t其它(CODE)\n");
            foreach (DataRow dr in dt.Rows)
            {
                //sw.WriteLine(dr[0] + "\t" + dr[1]);
                for (int i = 0; i < dt.Columns.Count; i++)
                {

                    sw.Write(dr[i] + "\t");
                }
                sw.Write("\n");
            }
            sw.Close();
            string sss = DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(10000).ToString() + ".xls";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + sss);
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            Response.Write(sw);
            Response.End(); 
        }
    }
}
